const { v4: uuidv4 } = require('uuid');
const db = require('../db/db');

exports.generateTestData = (count = 20) => {
    const priorities = ['low', 'medium', 'high'];
    const statuses = ['not-started', 'in-progress', 'cancelled', 'completed'];
    const prefixes = ['测试', 'Mock', 'DEMO']; // 通过前缀识别测试数据

    const stmt = db.prepare(`
    INSERT INTO tasks (
      id, title, description, priority, status, 
      start_date, due_date, created_at, feedback
    ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
  `);

    for (let i = 1; i <= count; i++) {
        const prefix = prefixes[Math.floor(Math.random() * prefixes.length)];
        // 在生成测试数据处修改
        const task = {
            id: uuidv4(),
            title: `${prefix}任务${i}`, // 添加可识别前缀
            description: `[测试数据] 这是自动生成的${prefix}任务`,
            priority: priorities[Math.floor(Math.random() * priorities.length)],
            status: statuses[Math.floor(Math.random() * statuses.length)],
            start_date: new Date().toISOString().split('T')[0], // 仅日期
            due_date: new Date(Date.now() + 86400000).toISOString().split('T')[0], // 仅日期
            created_at: new Date().toISOString().split('T')[0] // 仅日期
        };

        stmt.run(
            task.id,
            task.title,
            task.description,
            task.priority,
            task.status,
            task.start_date,
            task.due_date,
            task.created_at,
            task.feedback
        );
    }

    stmt.finalize();
    db.close(); // 添加关闭连接
};

exports.cleanTestData = (all = false) => {
    const cleanQuery = all ?
        'DELETE FROM tasks' :
        `DELETE FROM tasks WHERE description LIKE '[测试数据]%'`;

    db.run(cleanQuery, function (err) {
        if (err) console.error('清理失败:', err);
        else console.log(`已清理${this.changes}条数据`);
        // 在generateTestData和cleanTestData函数末尾添加：
        db.close(); // 这里已有关闭连接
    });
};
